package alorithm;

/**
 * 计算时针和分针的夹角
 * Created by yzy on 2020-12-25 19:34
 */
public class ClockAngle {

    public static void main(String[] args) {
        int hour = 0;
        int minute = 30;
        System.out.println(calculateAngle(hour, minute));
    }


    /**
     * 计算时针和分针的夹角
     * 思路：
     * 1、计算时针到0的夹角，计算分针到0的夹角。
     * 2、两个角度相减，就是时针和分针的夹角。记得要改为正整数且小于180度
     * @param hour
     * @param minute
     * @return
     */
    public static double calculateAngle(int hour, int minute){
        hour = hour >=12 ? hour-12 : hour;
        double hourAngle = (hour*60.0+minute) / (12*60) * 360.0;
        double minAngle = minute/60.0 * 360.0;
        double res = hourAngle - minAngle;
        res = Math.abs(res);
        res = res > 180 ? res-180 : res;
        return res;
    }
}
